package com.huidu.bitrade.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Lists;
import com.huidu.bitrade.util.RedisUtilUse;
import com.huidu.bitrade.vo.CoinThumbVO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
public class ThumbController {

    private static List<String> signList;
    @Autowired
    private RedisUtilUse redisUtilUse;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RestTemplate restTemplate;

    static {
        signList = Lists.newArrayList();
        signList.add("ur2us7janc3pdf");
        signList.add("ur2us63gda3pdf");
        signList.add("ur2us63gda3cfp");
    }

    /**
     * 获取指定交易对行情
     * @param sign
     * @param symbol
     * @return
     */
    @GetMapping("findSymbolThumb")
    public MessageResultVo<List<CoinThumbVO>> findSymbolThumbVO(@RequestParam("sign") String sign, @RequestParam("symbol") String symbol) {
        if(!signList.contains(sign)){
            return MessageResultVo.error("签名不正确");
        }
        String check = redisUtilUse.getCacheObject("interface:check:"+sign);
        if(StringUtils.isNotEmpty(check)){
            return MessageResultVo.error("请求太频繁，请稍后再试");
        }
        redisTemplate.opsForValue().set("interface:check:"+sign,"value",5, TimeUnit.SECONDS);

        String serviceName = "bitrade-market";
        String url = "http://" + serviceName + "/market/symbol-thumb";
        ResponseEntity<JSONArray> forEntity = restTemplate.getForEntity(url, JSONArray.class);
        List<CoinThumbVO> list = Lists.newArrayList();
        JSONArray body = forEntity.getBody();
        for (int i = 0; i < body.size(); i++) {
            JSONObject jsonObject = body.getJSONObject(i);
            if(symbol.equals(jsonObject.getString("symbol"))){
                CoinThumbVO coinThumbVO = JSONObject.parseObject(jsonObject.toJSONString(), CoinThumbVO.class);
                list.add(coinThumbVO);
                break;
            }
        }
        return MessageResultVo.success(list);
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class MessageResultVo<T> {

        private int code;

        private String message;

        private T data;

        public static MessageResultVo error(String msg){
            return new MessageResultVo(500,msg,"");
        }

        public static <T> MessageResultVo success(T data){
            return new MessageResultVo(0,"success",data);
        }
    }
}
